package com.pet.server.ext.action.square;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.pet.server.ext.ActionBase;
import com.pet.server.ext.ApiError;
import com.pet.server.ext.Result;
import com.pet.server.ext.data.Page;
import com.pet.server.ext.data.PetMissing;
import com.pet.server.ext.service.UserService;
import com.pet.server.ext.util.JsonUtil;
import com.pet.server.ext.util.Parameters;
import com.pet.server.ext.util.cache.ConstantUtil;

public class Missinglist extends ActionBase{

	public Integer startIndex,itemCount,user_id;
	public String queryString,city_name;
	public Page page;
	public Missinglist(){
		 needCheckAuth =false;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {	
		parseToParameter(params);
		city_name=params.getFieldValue(params.getFieldValue("city_name"));
		itemCount = p.itemCount==0?10:p.itemCount;
		page = new Page(p.startIndex, itemCount,p.queryString);
	}
	
	@Override
	public Result invokeService() throws ApiError {
		UserService userService = new UserService();
		userService.searchPetMissing(page, user_id, p.citycode, p.area,p.pet_cate,city_name);
		JSONArray array = new JSONArray();
		for(PetMissing pm:(List<PetMissing>)page.getList()){
			JSONObject o = new JSONObject();
			o.accumulate("image",pm.ico_path);
			o.accumulate("breed", pm.breed);
			o.accumulate("miss_id", pm.miss_id);
			o.accumulate("breed_name", ConstantUtil.getInstance().getName(pm.breed));
			o.accumulate("sex", pm.sex);
			o.put("miss_date", JsonUtil.getTime(pm.miss_date));
			o.put("city_name", pm.city_name);
			array.add(o);
		}
		/**
		for(PetMissing pm:(List<PetMissing>)page.getList()){
            List<Image> l = userService.queryImage(pm.miss_id, BaseAttribute.image_type.pet_missing.toString());
            if(l.size()>0){
            	pm.image=l.get(0).src;
            }
		}*/
		Result result = new Result();
        result.setPage4array(page, array);
	    return result;
	}


}
